package com.yue.ssa.controller;

import com.yue.ssa.bean.RespBean;
import com.yue.ssa.common.ExcelUtils;
import com.yue.ssa.service.impl.FileServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zhengyl
 * @description 文件管理
 * @date 2020-04-08 10:33
 */
@Api(value = "文件管理", tags = "文件管理")
@RequestMapping("rest/file")
@RestController
public class FileController {
    @Autowired
    private FileServiceImpl fileService;

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @PostMapping
    public RespBean upload(MultipartFile multipartFile) {
        return fileService.upload(multipartFile);
    }

    @ApiOperation(value = "从execl读取数据", notes = "从execl读取数据")
    @PostMapping("getDataFromExcel")
    public void getDataFromExcel(MultipartFile multipartFile) throws Exception {
        List<Row> rowList = ExcelUtils.getDataFromExcel(multipartFile);
        for (Row row : rowList) {
            int cells = row.getPhysicalNumberOfCells();//每一行对应的列数
            for (int i = 0; i < cells; i++) {
                Cell cell = row.getCell(i);
                if (cell == null) continue;
//                cell.setCellType(CellType.STRING);//设置表格的数据都当做String来读取
                CellType cellType = cell.getCellType();
                if (cellType == CellType.NUMERIC) {
                    System.out.print(row.getCell(i).getNumericCellValue() + " ");
                } else {
                    System.out.print(row.getCell(i).getStringCellValue() + " ");
                }
            }
            System.out.println();
        }
    }

    @ApiOperation(value = "下载文件", notes = "下载文件")
    @ApiImplicitParam(name = "id", value = "文件主键", required = true, dataType = "String")
    @GetMapping("/{id}")
    public void download(HttpServletResponse response, @PathVariable String id) {
        fileService.download(response, id);
    }

    @ApiOperation(value = "删除文件", notes = "删除文件")
    @ApiImplicitParam(name = "ids", value = "文件主键", required = true, allowMultiple = true, dataType = "String")
    @DeleteMapping("/delete")
    public RespBean delete(@RequestParam String[] ids) {
        return fileService.delete(ids);
    }

    @ApiOperation(value = "根据关联主键获取文件", notes = "根据关联主键获取文件")
    @ApiImplicitParam(name = "relatedId", value = "关联主键", required = true, dataType = "String")
    @GetMapping("/getFilesByRelatedId")
    public RespBean getFilesByRelatedId(@RequestParam String relatedId) {
        return fileService.getFilesByRelatedId(relatedId);
    }

    @ApiOperation(value = "批量下载文件", notes = "批量下载文件")
    @ApiImplicitParam(name = "ids", value = "文件主键", required = true, allowMultiple = true, dataType = "String")
    @GetMapping("/batchDownload")
    public void batchDownload(HttpServletResponse response, @RequestParam String[] ids) {
        fileService.batchDownload(response, ids);
    }

    @ApiOperation(value = "获取文件流", notes = "获取文件流")
    @ApiImplicitParam(name = "paths", value = "路径集合", required = true, allowMultiple = true, dataType = "String")
    @GetMapping("/getInputStream")
    public List getInputStream(@RequestParam String[] paths) {
        return fileService.getInputStream(paths);
    }
}
